import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by asus on 2017/11/21.
 */
public class ListGroup<T> {
    private class VertexNode<T>{
            T Data;
            EdgeNode<T> firstIn;
            EdgeNode<T> firstOut;
            public VertexNode(T data){
                this.Data = data;
                this.firstIn = null;
                this.firstOut = null;
            }
    }
    private class EdgeNode<T>{
            int tailVex;
            int headVex;
            EdgeNode<T> headLink;
            EdgeNode<T> tailLink;

            int weight;
            public  EdgeNode(int tailVex,int headVex,EdgeNode headLink,EdgeNode tailLink){
                this.tailVex = tailVex;
                this.headVex = headVex;
                this.headLink = headLink;
                this.tailLink = tailLink;

                weight = 0;
            }
            public  EdgeNode(int tailVex,int headVex,EdgeNode headLink,EdgeNode tailLink,int weight){
                this.tailVex = tailVex;
                this.headVex = headVex;
                this.headLink = headLink;
                this.tailLink = tailLink;
                this.weight = weight;
            }
            public void insertTail(EdgeNode<T> node){
                if (tailLink!=null){
                    EdgeNode<T> myNode = this.tailLink.tailLink;
                    myNode.insertTail(node);
                }
                else
                    tailLink = node;

            }
            public void insertHead(EdgeNode<T> node){
                if (headLink!=null){
                    EdgeNode<T> myNode =this.headLink.headLink;
                    myNode.insertHead(node);
                }
                else
                    headLink = node;
            }
    }

    private ArrayList<VertexNode> NodeList;

    private int NumOfEdges;
    public ListGroup(int n){
        NodeList = new ArrayList<>(n);
        NumOfEdges=0;
    }
    public void insertNode(T data){
        VertexNode<T> myNode = new VertexNode(data);
        NodeList.add(myNode);
    }
    public void insertEdge(int tail,int head){
        EdgeNode<T> myEdge = new EdgeNode(tail,head,null,null);
        VertexNode<T> tailNode = NodeList.get(tail);
        VertexNode<T> headNode = NodeList.get(head);
        if (tailNode.firstOut==null){
            tailNode.firstOut = myEdge;
        }
        else {
            tailNode.firstOut.insertTail(myEdge);
        }
        if (headNode.firstIn==null){
            headNode.firstIn = myEdge;
        }
        else {
           headNode.firstIn.insertHead(myEdge);
        }
    }
    public Iterator<T> iteratorBFS(int index){
        return null;
    }
    public void getTail(int index){
        EdgeNode<T> myNode = NodeList.get(index).firstOut;
        System.out.print(NodeList.get(index).Data+":");
        while (myNode!=null){
            System.out.print(NodeList.get(myNode.headVex).Data+"-->"+NodeList.get(myNode.tailVex).Data+"|");
            myNode = myNode.tailLink;
        }
    }
    public void getHead(int index){
        EdgeNode<T> myNode = NodeList.get(index).firstIn;
        System.out.print(NodeList.get(index).Data+":");
        while (myNode!=null){
            System.out.print(NodeList.get(myNode.headVex).Data+"-->"+NodeList.get(myNode.tailVex).Data+"|");
            myNode = myNode.headLink;
        }
    }

    public static void main(String[] args) {
        ListGroup<String> myGroup = new ListGroup<>(4);
        myGroup.insertNode("A");
        myGroup.insertNode("B");
        myGroup.insertNode("C");
        myGroup.insertNode("D");
        myGroup.insertEdge(0,2);
        myGroup.insertEdge(0,1);
        myGroup.insertEdge(3,0);
        myGroup.insertEdge(3,1);
        myGroup.getTail(0);
        System.out.print("\n");
        myGroup.getHead(1);


    }
}
